<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>日报修改</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{../../lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{../../css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" th:object="${health}">
    <div class="layui-form-item">
            <label class="layui-form-label required">上传日期</label>
            <div class="layui-input-inline">
                <input type="text" th:field="*{id}" style="display: none"/>
                <input type="text" th:field="*{createdate}" autocomplete="off" class="layui-input date" readonly="readonly" disabled="disabled">
            </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">员工姓名</label>
        <div class="layui-input-block" style="width: 190px">
            <input type="text" th:field="*{employee_name}" name="emp" readonly="readonly" disabled="disabled" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">温度</label>
        <div class="layui-input-block" style="width: 190px">
            <input type="text" th:field="*{temperature}" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">健康码</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="upload1">
                上传图片
            </button>
            <input type="hidden" id="pic" name="pic"th:value="${obj?.pic}"/>
            <!-- 预览区域 -->
            <div id="imgDiv" class="layui-upload-list" style="display: none;">
                <img class="layui-upload-img" width="100px" height="80px" id="demo1" name="msg"/>
                <p id="demoText"></p>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">行程码</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="upload2">
                上传图片
            </button>
            <input type="hidden" id="pic2" name="pic2"th:value="${obj?.pic}"/>
            <!-- 预览区域 -->
            <div id="imgDiv2" class="layui-upload-list" style="display: none;">
                <img class="layui-upload-img" width="100px" height="80px" id="demo2" name="msg"/>
                <p id="demoText2"></p>
            </div>
        </div>
    </div>






<div class="layui-form-item">
    <div class="layui-input-block">
        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">修改日报</button>
    </div>
</div>
</div>
<script th:src="@{../../lib/layui/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form', 'laydate','upload'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer,
            upload = layui.upload;
            $ = layui.$;

        laydate.render({
            elem: '#createdate',
            type: 'date'
            //指定元素
        });

        //健康吗
        var uploadInst = upload.render({
            elem: '#upload1', //绑定元素
            url: '/health/uploadFile', //上传接口
            before: function(obj){
                // var emp2=document.getElementsByClassName(emp).value;
                this.data = {
                    emp:document.getElementById("employee_name").value,
                    date:document.getElementById("createdate").value
                }
                //预读本地文件示例
                obj.preview(function(index, files, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res){
                //如果上传失败
                if(res){
                    //上传成功
                    layer.alert("上传成功"+res.url);
                    document.getElementById("pic").value = res.url;
                    $("#imgDiv").css("display","")// 显示图片框
                } else {
                    return layer.msg('上传失败');
                }
            },
            error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

        //行程码
        var uploadInst = upload.render({
            elem: '#upload2', //绑定元素
            url: '/health/uploadFile2', //上传接口
            before: function(obj){
                // var emp2=document.getElementsByClassName(emp).value;
                this.data = {
                    emp:document.getElementById("employee_name").value,
                    date:document.getElementById("createdate").value
                }
                //预读本地文件示例
                obj.preview(function(index, files, result){
                    $('#demo2').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res){
                //如果上传失败
                if(res){
                    //上传成功
                    layer.alert("上传成功"+res.url);
                    document.getElementById("pic2").value = res.url;
                    $("#imgDiv2").css("display","")// 显示图片框
                } else {
                    return layer.msg('上传失败');
                }
            },
            error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText2');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });



        form.on('submit(saveBtn)', function (data) {
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            $("#btn-submit").attr("disabled", "disabled").addClass("layui-btn-disabled");
            //提交数据
            var url = "/health/update";
            $.post(url, data.field, function (res) {
                console.log(res);
                if (res.code === 0) {
                    //成功提示
                    layer.msg(res.message, {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        //关闭窗口,刷新数据表格
                        parent.refreshTable();
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    });
                } else {
                    $("#btn-submit").removeAttr("disabled", "disabled").removeClass("layui-btn-disabled");
                    //失败则提示错误消息,窗口不关闭
                    layer.msg('考勤修改失败,请联系管理员', {
                        icon: 2,
                        time: 2000, //2秒关闭（如果不配置，默认是3秒）
                        anim: 6
                    }, function () {
                        //do something
                    });
                }
            })
            //关掉窗口并刷新
            //失败则提示错误信息
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>
</body>
</html>